require 'test/unit'
require 'engine/game_engine'
require 'engine/logic'

# to make test indempotent# {{{
class VectorDatabase
    def reset; @container = [] end
end
class MapDatabase
    def reset; @container = {} end
end
class TemplateInstanceDatabase
    def reset
        @instances.reset
        @templates.reset
    end
end# }}}

class Interceptor < Logic
    applicable_to Character
    properties :queue
    invoked_by :*
    def do_logic
        self.queue ||= []
        self.queue.unshift self.action_type
        true
    end
end

class GameMechanicsTest < Test::Unit::TestCase
    def setup
        @region = Region.new
        @region.name = 'Region'
        RegionDatabase.instance.add(@region)

        @room = Room.new
        @room.name = 'Room'
        @room.region = @region
        @region.add_room(@room)
        RoomDatabase.instance.add(@room)

        @char = Character.new
        @char.name = 'Test'
        @char.room = @room
        @char.region = @region
        CharacterDatabase.instance.add_instance(@char)
        @intercept = Interceptor.new(@char)
        @char.add_logic(@intercept)

        @char2 = Character.new
        @char2.name = 'Test 2'
        @char2.room = @room
        @char2.region = @region
        CharacterDatabase.instance.add_instance(@char2)
        @intercept2 = Interceptor.new(@char2)
        @char2.add_logic(@intercept2)
    end
    def teardown
        CharacterDatabase.instance.reset
        RoomDatabase.instance.reset
        RegionDatabase.instance.reset
    end
    def test_login
        GameEngine.instance.login(@char)
        assert(@char.logged_in)
        assert_equal(@room,@char.room)
        assert_equal(@region,@char.region)
        assert(@room.characters.include?(@char))
        assert(@region.characters.include?(@char))
        assert_equal(:enterroom,@intercept.queue.shift) 
        assert_equal(:enterregion,@intercept.queue.shift) 
    end
    def test_login_2
        GameEngine.instance.login(@char)
        GameEngine.instance.login(@char2)

        assert_equal(:enterroom,@intercept.queue.shift) # char2
        assert_equal(:enterroom,@intercept.queue.shift) 
        assert_equal(:enterregion,@intercept.queue.shift)  # char2
        assert_equal(:enterregion,@intercept.queue.shift) 

        assert_equal(:enterroom,@intercept2.queue.shift) 
        assert_equal(:enterregion,@intercept2.queue.shift) 
    end
    def test_logout
        GameEngine.instance.login(@char)
        GameEngine.instance.logout(@char)
        assert_equal(false,@char.logged_in)
        assert_equal(@room,@char.room)
        assert_equal(@region,@char.region)
        assert(! @room.characters.include?(@char))
        assert(! @region.characters.include?(@char))
        assert_equal(:leaveregion,@intercept.queue.shift) 
        assert_equal(:leaveroom,@intercept.queue.shift) 
    end
    def test_say
    end
end
